In [ ]:
aString = "I am string"
aString

In [ ]:
aString = 'python is fun'
aString
len(aString)

序列


In [ ]:
# 切片
print aString[2]
print aString[0:5]
print aString[:5]
print aString[-3:]

In [ ]:
# 修改,注意:字符串是不可变的
aString = aString[:10] + 'cool'
aString

In [ ]:
# 连接
print aString + aString
print aString * 3

In [ ]:
print aString
print 'a' in aString
print 'python' in aString
print 'cool' not in aString

In [ ]:
len(aString)

In [ ]:
enumerate(aString)

In [ ]:
for i, s in enumerate(aString):
    print i, s

% 操作符


In [ ]:
print "string: %s." % "wo, python is cool"
print 'string: "%s".' % "this is so cool"

In [ ]:
print "int: %d." % 123456
print "int: %+d." % 123456
print "int: %+d." % -123456
print "int: %-d." % 123456

In [ ]:
print "float: %f." % 12.3456
print "float: %.2f." % 12.3456

In [ ]:
print "MM/DD/YY = %02d/%02d/%d" % (12, 5, 2013)
print "MM/DD/YY = %02d/%02d/%d" % (12, 25, 2013)

原始字符


In [ ]:
# 换行符 \n
print "python is cool.\npython is fun"

In [ ]:
# tab \t
print "python is cool.\n\tpython is fun"

内建方法


In [ ]:
print aString
print aString.capitalize()

In [ ]:
print aString.isalpha()
print "python".isalpha()
print aString.isdigit()
print "123456".isdigit()

In [ ]:
print ", ".join(aString)

In [ ]:
print aString.upper()
print "PYTHON".lower()

In [ ]:
tempString = "            wo python                "
print tempString.lstrip()
print tempString.rstrip()
print tempString.strip()

In [ ]:
print aString
print aString.replace("cool", "fun")

In [ ]:
print aString.split(" ")
print aString.split("is")

In [ ]:
print aString
print aString.startswith("python")
print aString.endswith("cool")

三引号


In [ ]:
aString = """python is cool.
python is fun."""
aString

In [ ]:
aString = '''python is cool.
python is fun.
python is awesome.'''
aString

Unicode


In [ ]:
strChinese = "编程"
print strChinese

In [ ]:
print strChinese.decode("utf-8")

In [ ]:
print strChinese.decode("utf-8")

In [ ]:
print u"编程"
print unicode("编程", "utf-8")

In [ ]:
print type(strChinese)
strChinese = unicode("编程", "utf-8")
type(strChinese)

string 模块


In [ ]:
import string

In [ ]:
string.ascii_letters

In [ ]:
string.ascii_lowercase

In [ ]:
string.ascii_uppercase

In [ ]:
string.digits

字符串模板


In [ ]:
strTemplate = string.Template("${what} is ${how}")

In [ ]:
print strTemplate.substitute(what="Python", how="cool")
print strTemplate.substitute(what="Python", how="fun")
print strTemplate.substitute(what="String", how="cool")
print strTemplate.substitute(what="String", how="fun")

In [ ]:
# --------------------------------------------------------------------------------  
# Copyright (c) 2013 Mack Stone. All rights reserved.  
#   
# Permission is hereby granted, free of charge, to any person obtaining a copy  
# of this software and associated documentation files (the "Software"), to deal  
# in the Software without restriction, including without limitation the rights  
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
# copies of the Software, and to permit persons to whom the Software is  
# furnished to do so, subject to the following conditions:  
#   
# The above copyright notice and this permission notice shall be included in  
# all copies or substantial portions of the Software.  
#   
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
# THE SOFTWARE.  
# --------------------------------------------------------------------------------